package com.yanqu.road.server.gameplayer.module.activity.unionchest;

import com.yanqu.road.entity.activity.unionChest.UnionChestUserData;
import com.yanqu.road.entity.activity.unionChest.config.UnionChestBoxConfig;
import com.yanqu.road.entity.activity.unionChest.config.UnionChestConfig;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.UnionChestProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.unionchest.UnionChestMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.Date;

public class UnionChestModule extends GeneralModule {


    public UnionChestModule(GamePlayer player) {
        super(player);
    }

    @Override
    public void afterLogin() {
        userLogin();
    }

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        return true;
    }

    /**
     * 刷新活动 || 登录
     */
    public void userLogin() {
        syncUserData();
    }

    public void initSystem() {
        syncUserData();
    }

    // 同步玩家数据
    public void syncUserData() {
        if (!SystemOpenMgr.systemOpen(player, eSystemId.UnionChest.getValue())) {
            return;
        }
        // 上跨服吧，还有一些跨服数据
        UnionChestConfig config = UnionChestMgr.getConfig();
        if (config != null && ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            UnionChestProto.CrossUnionChestSyncUserReqMsg.Builder crossMsg = UnionChestProto.CrossUnionChestSyncUserReqMsg.newBuilder();
            crossMsg.setActivityId(config.getActivityInfo().getActivityId());
            crossMsg.setUnionId(player.getUserInfo().getUnionUid());
            crossMsg.setReqType(ClientProtocol.U_UNIONCHEST_USER_DATA_SYNC);
            player.sendPacket(CrossProtocol.C2_UNIONCHEST_SYNC_USER_DATA, crossMsg);
        }
    }

    public void reqUserData() {
        if (!SystemOpenMgr.systemOpen(player, eSystemId.UnionChest.getValue())) {
            return;
        }
        // 上跨服吧，还有一些跨服数据
        UnionChestConfig config = UnionChestMgr.getConfig();
        if (config != null && ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            UnionChestProto.CrossUnionChestSyncUserReqMsg.Builder crossMsg = UnionChestProto.CrossUnionChestSyncUserReqMsg.newBuilder();
            crossMsg.setActivityId(config.getActivityInfo().getActivityId());
            crossMsg.setUnionId(player.getUserInfo().getUnionUid());
            crossMsg.setReqType(ClientProtocol.U_UNIONCHEST_USER_DATA_REQ);
            player.sendPacket(CrossProtocol.C2_UNIONCHEST_SYNC_USER_DATA, crossMsg);
        } else {
            UnionChestProto.UnionChestUserRespMsg.Builder builder = UnionChestProto.UnionChestUserRespMsg.newBuilder();
            player.sendPacket(ClientProtocol.U_UNIONCHEST_USER_DATA_REQ, builder);
        }
    }

    /**
     * 分享宝箱
     */
    public int shareBox(int activityId, int boxType) {
        UnionChestConfig config = UnionChestMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo()) || config.getActivityInfo().getActivityId() != activityId) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }

        UnionChestBoxConfig boxConfig = config.getBoxMap().get(boxType);
        if (null == boxConfig) {
            return GameErrorCode.E_UNIONCHEST_BOX_NOT_FOUND;
        }

        if (StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())) {
            return GameErrorCode.E_UNION_NO_JOIN_UNION;
        }

        Property cost = new Property();
        cost.addProperty(boxConfig.getBoxId(), BigInteger.ONE);
        if (!player.getModule(CurrencyModule.class).currencyIsEnough(cost)) {
            return GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
        }
        player.getModule(PreReduceModule.class).preReduce(cost);

        UnionChestProto.CrossUnionChestShareBoxReqMsg.Builder crossReq = UnionChestProto.CrossUnionChestShareBoxReqMsg.newBuilder();
        crossReq.setActivityId(activityId);
        crossReq.setBoxType(boxType);
        crossReq.setUnionId(player.getUserInfo().getUnionUid());
        crossReq.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        player.sendPacket(CrossProtocol.C2_UNIONCHEST_BOX_SHARE, crossReq);
        return 0;
    }

    public int openBox(UnionChestProto.UnionChestBoxOpenBoxReqMsg reqMsg) {
        int activityId = reqMsg.getActivityId();
        UnionChestConfig config = UnionChestMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo()) || config.getActivityInfo().getActivityId() != activityId) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }

        if (StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())) {
            return GameErrorCode.E_UNION_NO_JOIN_UNION;
        }


        UnionChestProto.CrossUnionChestOpenBoxReqMsg.Builder crossReq = UnionChestProto.CrossUnionChestOpenBoxReqMsg.newBuilder();
        crossReq.setActivityId(activityId);
        crossReq.setReqMsg(reqMsg);
        crossReq.setUserName(player.getUserInfo().getNickName());
        crossReq.setUnionId(player.getUserInfo().getUnionUid());
        crossReq.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        player.sendPacket(CrossProtocol.C2_UNIONCHEST_BOX_OPEN, crossReq);
        return 0;
    }

    public int getNoticeList(UnionChestProto.UnionChestNoticeListReqMsg reqMsg) {
        int activityId = reqMsg.getActivityId();
        UnionChestConfig config = UnionChestMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo()) || config.getActivityInfo().getActivityId() != activityId) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        UnionChestProto.CrossUnionChestNoticeReqMsg.Builder crossReq = UnionChestProto.CrossUnionChestNoticeReqMsg.newBuilder();
        crossReq.setActivityId(activityId);
        crossReq.setUnionId(player.getUserInfo().getUnionUid());
        player.sendPacket(CrossProtocol.C2_UNIONCHEST_BOX_NOTICE, crossReq);
        return 0;
    }

}
